<template>
  <div>
    <el-dialog
      :title="isAdd ? '添加商品分类' : '编辑商品分类'"
      center
      :visible.sync="isShow"
      :before-close="cancel"
      :modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="pid" label="上级分类" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择上级分类">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in catelist"
              :key="item.id"
              :value="item.id"
              :label="item.catename"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="catename"
          label="分类名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="图片"
          :label-width="formLabelWidth"
          v-if="form.pid != 0"
        >
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove="onRemove"
            :file-list="fileList"
            :on-change="changeFile"
            :limit="1"
            :on-exceed="handleExceed"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog
            :visible.sync="dialogVisible"
            :modal-append-to-body="false"
          >
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#409EFF"
            inactive-color="#DCDFE6"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { cateAdd, cateEdit, cateInfo } from "@/request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: "",
      fileList: [],
      form: {
        pid: 0, //上级分类编号
        catename: "", // 商品分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1, //状态    1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        catename: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },
    };
  },
  computed: {
    ...mapGetters({
      catelist: "cate/getCateList",
    }),
  },
  methods: {
    ...mapActions({
      getCate: "cate/changeCateAction",
    }),
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    changeFile(file, filelist) {
      console.log(file, filelist, "触发");
      this.form.img = file.raw;
    },
    onRemove(file, filelist) {
      console.log(file, filelist);
    },
    onPreview(file) {
      console.log(file);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    reset() {
      this.form = {
        pid: 0, //上级分类编号
        catename: "", // 商品分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1, //状态    1正常2禁用
      };
      this.$refs.form.resetFields();
      this.fileList = [];
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    lookup(id) {
      cateInfo({
        id,
      }).then((res) => {
        console.log(res, "一条数据查询");
        if (res.code == 200) {
          this.form = res.list;
          this.form.id = id;
          this.fileList = [{ url: this.$imgUrl + res.list.img }];
        }
      });
    },
    comfirm() {
      this.$refs.form.validate((flag) => {
        console.log(flag);
        if (flag) {
          console.log(this.form);
          if (this.isAdd) {
            cateAdd(this.form).then((res) => {
              console.log(res, "商品分类添加");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getCate();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            // 编辑
            cateEdit(this.form).then((res) => {
              console.log(res, "商品分类编辑");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getCate();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          alert("验证失败");
        }
      });
    },
  },
};
</script>

<style lang="less"scoped>
</style>
